package site.beanyon.now.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.beanyon.now.constant.ResponseEnum;
import site.beanyon.now.model.dto.HttpResponse;
import site.beanyon.now.model.dto.TimeCardDTO;
import site.beanyon.now.service.AnalysisService;
import site.beanyon.now.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 数据分析控制器
 */
@RestController
@RequestMapping("/analysis")
public class AnalysisController {
    @Autowired
    private AnalysisService analysisService;

    /**
     * 根据起止日期，获取时间卡片及对应的时间区间数据
     *
     * @param params 参数Map
     * @return 时间卡片列表
     */
    @PostMapping("/data")
    private HttpResponse getAnalysisData(@RequestBody Map<String, String> params) {
        String startDate = params.get("startDate");
        String endDate = params.get("endDate");

        // 对数据判空
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            return new HttpResponse(ResponseEnum.REQUIRED_PARAM_NOT_EXISTS, null);
        }

        // 获取给定时间范围内的时间卡片
        List<TimeCardDTO> timeCardDTOList = this.analysisService.getAllData(startDate, endDate);

        if (timeCardDTOList != null) {
            return new HttpResponse(ResponseEnum.SUCCESS, timeCardDTOList);
        }

        return new HttpResponse(ResponseEnum.FAILED, null);
    }

    /**
     * 获取当前用户的全部activity数据
     *
     * @return activity列表
     */
    @GetMapping("/get-all-activities")
    private HttpResponse getAllData() {
        // 获取用户总花费时间及全部任务列表
        Map<String, Object> result = this.analysisService.listAllActivity();
        return new HttpResponse(ResponseEnum.SUCCESS, result);
    }
}
